import Vue from 'vue';
import Vuex from 'vuex';
import api from '@/assets/js/api.js';
import { Toast, Dialog } from 'vant';

Vue.use(Vuex);
Vue.use(Toast).use(Dialog);
Vue.prototype.$api = api.post;

export default new Vuex.Store({
  state: {
    userInfo: {},
    appClassName: true,
    obj: {
      title: '登录',
      type: 'login',
      submit: '登录',
    },
    storageTimes: {
      time: 300,
      endTime: '',
    },
  },
  mutations: {
    setUserInfo: function(state, flag) {
      state.userInfo = flag;
    },
    setAppClassName: function(state, flag) {
      state.appClassName = flag;
      localStorage.setItem('appClassName', JSON.stringify(flag));
    },
    setUserInfo: function(state, flag) {
      state.userInfo = flag;
    },
    setObj: function(state, flag) {
      state.obj = flag;
      localStorage.setItem('obj', JSON.stringify(flag));
    },
    // 设置过期时间
    setStorageTimes: function(state, flag = 1) {
      state.storageTimes.endTime =
        state.storageTimes.time * flag * 1000 + new Date().getTime();
      localStorage.setItem('storageTimes', JSON.stringify(state.storageTimes));
    },
    // 登录
    setLogin: function(state, flag) {
      Toast({
        message: flag.msg,
        duration: 1500,
      });
      setTimeout(() => {
        state.userInfo = flag.data;
        flag.data.remind = flag.data.remind === 1 ? true : false;
        localStorage.setItem('userInfo', JSON.stringify(flag.data));
        router.push('/cx/index');
      }, 1600);
    },
    // 注册
    setRegister: function(state, flag) {
      Toast({
        message: flag.msg,
        duration: 1600,
      });
      let obj = {
        title: '登录',
        type: 'login',
        submit: '登录',
      };
      setTimeout(() => {
        state.obj = obj;
        localStorage.setItem('obj', JSON.stringify(obj));
      }, 1700);
    },
    // 找回密码
    setRecoverpwd: function(state, flag) {
      Toast({
        message: flag,
        duration: 1600,
      });
      setTimeout(() => {
        let obj = {
          title: '登录',
          type: 'login',
          submit: '登录',
        };
        state.obj = obj;
        localStorage.setItem('obj', JSON.stringify(obj));
      }, 1700);
    },
  },
  actions: {
    ON_USERINFO: function(context, flag) {
      context.commit('setUserInfo', flag);
    },
    // 设置当前平台
    ON_APPCLASSNAME: function(context, flag) {
      context.commit('setAppClassName', flag);
    },
    // 设置登录页状态
    ON_SETOBJ: function(context, flag) {
      context.commit('setObj', flag);
    },
    // 登录
    ON_LOGIN: function(context, flag) {
      context.commit('setLogin', flag);
    },
    // 注册
    ON_REGISTER: function(context, flag) {
      context.commit('setRegister', flag);
    },
    // 找回密码
    ON_RECOVERPWD: function(context, flag) {
      context.commit('setRecoverpwd', flag);
    },
    // 设置过期时间
    ON_STORAGETIMES: function(context, flag) {
      context.commit('setStorageTimes', flag);
    },
  },
  getters: {
    userInfo: (state) => state.userInfo,
  },
  modules: {},
});
